//
//  BaseYueduController.m
//  网易新闻
//
//  Created by dllo on 15/10/7.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "BaseYueduController.h"
@interface BaseYueduController ()
@property(nonatomic, retain)UIScrollView *scoll;
@property(nonatomic, retain)UIView *line;
@end

@implementation BaseYueduController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.button_a = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button_a.frame = CGRectMake(15, 14, 80, 28);
        self.button_a.selected = YES;
        self.button_a.tintColor = [UIColor clearColor];
        [self.button_a setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [self.button_a setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [self.button_a addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
        
        self.button_b = [UIButton buttonWithType:UIButtonTypeSystem];
        self.button_b.frame = CGRectMake(95, 14, 80, 28);
        self.button_b.tintColor = [UIColor clearColor];
        [self.button_b setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [self.button_b setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [self.button_b addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
        
        self.line = [[UIView alloc]initWithFrame:CGRectMake(15, 42, 80, 2)];
        self.line.backgroundColor = [UIColor whiteColor];
        
        [self setButtonTitle];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.button_a.hidden = NO;
    self.button_b.hidden = NO;
    self.line.hidden = NO;
}

- (void)viewWillLayoutSubviews{
    [self.navigationController.navigationBar addSubview:self.button_a];
    [self.navigationController.navigationBar addSubview:self.button_b];
    [self.navigationController.navigationBar addSubview:self.line];
    [self.line release];
}

- (void)viewWillDisappear:(BOOL)animated{
    self.button_a.hidden = YES;
    self.button_b.hidden = YES;
    self.line.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.controller_a.view.frame = CGRectMake(0, 0, 375, 560);
    self.controller_b.view.frame = CGRectMake(375, 0, 375, 560);
    [self addChildViewController:self.controller_a];
    [self addChildViewController:self.controller_b];
    
    self.scoll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 375, 560)];
    self.scoll.contentSize = CGSizeMake(750, 1);
    self.scoll.showsHorizontalScrollIndicator = NO;
    self.scoll.showsVerticalScrollIndicator = NO;
    self.scoll.bounces = NO;
    self.scoll.pagingEnabled = YES;
    [self.scoll addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    [self.scoll addSubview:self.controller_a.view];
    [self.scoll addSubview:self.controller_b.view];
    [self.view addSubview:self.scoll];
    
    [self.scoll release];
}

- (void)choose:(UIButton *)button{
    if (!button.selected) {
        self.button_a.selected = !self.button_a.selected;
        self.button_b.selected = !self.button_b.selected;
        if (self.button_a.selected) {
            [self.scoll setContentOffset:CGPointMake(0, 0) animated:YES];
        }else{
            [self.scoll setContentOffset:CGPointMake(375, 0) animated:YES];
        }
    }
}

- (void)setButtonTitle{

}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    float content = [[change objectForKey:@"new"] CGPointValue].x;
    if (content<10) {
        self.button_a.selected = YES;
        self.button_b.selected = NO;
    }else if (content>365){
        self.button_a.selected = NO;
        self.button_b.selected = YES;
    }
    self.line.frame = CGRectMake(15+content/375.f*80, 42, 80, 2);
}

- (void)dealloc{
    [_scoll removeObserver:self forKeyPath:@"contentOffset"];
    [_controller_b release];
    [_controller_a release];
    [_scoll release];
    [_line release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
